package IOStream.FileIOStream;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class FileIO {

    private File file;

    public FileIO(String filepath){
        file = new File(filepath);
    }

    public String fioPathSeparator() {

        System.out.println("Windows系统文件路径符号：");

        //文件名称分隔符
        System.out.println("文件名称分隔符：\" "+File.separator+" \"");

        //路径分隔符
        System.out.println("路径分隔符：\" "+File.pathSeparator+" \"");







        return null;
    }

    public String fioFileinfo(){
        System.out.println("文件路径："+ file);
        System.out.println("文件绝对路径"+file.getAbsolutePath());
        System.out.println("文件名："+file.getName());
        System.out.println("文件大小："+file.length()+" Byte");
        System.out.println("文件是否存在："+file.exists());
        System.out.println("是否为文件："+file.isFile());
        System.out.println("是否为目录："+file.isDirectory());
        return null;
    }

    public String fioFileCtrl(){

        try{
            System.out.println("文件不存在并创建："+file.createNewFile());
            System.out.println("目录不存在并创建："+file.mkdirs());

            System.out.println("文件重命名为原文件名："+file.renameTo(file.getAbsoluteFile()));

            boolean isNull =false; if(file.length()==0){file.deleteOnExit(); isNull = true;}
            System.out.println("文件为空则退出程序时删除"+isNull);

            System.out.print("本路径下的所有文件和目录名：");
            for (String fName:new File(file.getParent()).list()) {
                System.out.print(fName+" ");
            }
            System.out.println();

            System.out.print("本路径下的所有文件：");
            for (File fN:new File(file.getParent()).listFiles()) {
                System.out.print(fN+" ");
            }
            System.out.println();

        }catch (Exception e) {
            System.err.println(e);
        }finally {

        }


        return null;
    }

    public void fioAllFileName(){
        System.out.println("遍历文件 start");
        fioAllFileNameFUN(file);
        System.out.println("遍历文件 end");
    }
    public void fioAllFileNameFUN(File fN){
        System.out.println(fN.getAbsoluteFile());
        if(fN.isDirectory()){

            File[] fNs = fN.listFiles();

            for (File fi: fNs) {
                fioAllFileNameFUN(fi);
            }
        }else{
            return;
        }
    }

    public void fioSearchFileName(String fileName){
        System.out.println("搜索文件 start");
        fioSearchNameFUN(file,fileName);
        System.out.println("搜索文件 end");
    }
    public void fioSearchNameFUN(File fN,String fileName){

        if(fN.getName().toUpperCase().indexOf(fileName.toUpperCase())>=0){
            System.out.println(fN.getAbsoluteFile());
        }

        if(fN.isDirectory()){

            File[] fNs = fN.listFiles();

            for (File fi: fNs) {
                fioSearchNameFUN(fi,fileName);
            }
        }else{
            return;
        }
    }

}
